package com.codecash.mapper;

import com.codecash.entity.TaskOrder;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface TaskOrderMapper {
    @Insert("INSERT INTO orders (task_id, user_id, accepted_time, status) " +
            "VALUES (#{taskId}, #{userId}, #{acceptedTime}, '进行中')")
    void createOrder(@Param("taskId") Long taskId, 
                    @Param("userId") Long userId, 
                    @Param("acceptedTime") LocalDateTime acceptedTime);

    // 查询用户的所有订单
    @Select("SELECT o.*, t.title as taskTitle, t.content as taskContent, t.budget " +
            "FROM orders o " +
            "LEFT JOIN tasks t ON o.task_id = t.id " +
            "WHERE o.user_id = #{userId} " +
            "ORDER BY o.accepted_time DESC")
    List<TaskOrder> getUserOrders(Long userId);

    // 更新订单状态
    @Update("UPDATE orders SET status = #{status} WHERE id = #{orderId} AND user_id = #{userId}")
    int updateOrderStatus(@Param("orderId") Long orderId, 
                         @Param("userId") Long userId, 
                         @Param("status") String status);

    // 获取单个订单详情
    @Select("SELECT o.*, t.title as taskTitle, t.content as taskContent, t.budget " +
            "FROM orders o " +
            "LEFT JOIN tasks t ON o.task_id = t.id " +
            "WHERE o.id = #{orderId}")
    TaskOrder getOrderById(Long orderId);

    // 通过taskId获取订单
    @Select("SELECT o.*, t.title as taskTitle, t.content as taskContent, t.budget " +
            "FROM orders o " +
            "LEFT JOIN tasks t ON o.task_id = t.id " +
            "WHERE o.task_id = #{taskId}")
    TaskOrder getOrderByTaskId(Long taskId);

    // 通过taskId更新订单状态
    @Update("UPDATE orders SET status = #{status} WHERE task_id = #{taskId} AND user_id = #{userId}")
    int updateOrderStatusByTaskId(@Param("taskId") Long taskId, 
                                @Param("userId") Long userId, 
                                @Param("status") String status);
} 